<?php
declare (strict_types = 1);
namespace app\mini\controller;

use app\BaseController;
use think\facade\Cache;
use think\facade\Db;


/**
 * Class Index
 * @package app\mini\controller
 */
class Yunma extends BaseController
{
	/**
	 * 商品列表
     * @return Json
     * @author Mr.Li
     * @create 2021-06-21 14:25
	 */
    public function getPhonecode()
    {
      $paramArr  = request()->param();
      $phone = $paramArr['phone'] ?? '';
      $type  = $paramArr['type'] ?? '';
      if (empty($phone)) {
        return ['code'=>0,'msg'=>'请输入手机号'];
      }
      if (empty($type) && in_array($type,[1,2])) {
        return ['code'=>0,'msg'=>'请输入手机号'];
      }
      $time  = time()-1*60;
      $where = [];
      // array_push($where,['code','=',$code]);
      array_push($where,['phone','=',$phone]);
      array_push($where,['type','=',$type]);
      array_push($where,['create_time','>=',$time]);
      $res = Db::name('phone_code')->where($where)->find();
      if ($res) {
        return ['code'=>$code,'msg'=>'一分钟内不可重复获取验证码'];
      }
      $data = $this->getCode($phone,$type);
      return $data;
    }
  function send_post($url, $post_data){
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_URL, $url);
      /**配置返回信息**/
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//获取的信息以文件流的形式返回，不直接输出
      curl_setopt($curl, CURLOPT_HEADER, 0);//不返回header部分
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //禁止 cURL 验证对等证书
      curl_setopt($curl, CURLOPT_POST, 1); //支持post提交数据
      curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
      $code = 200; //执行成功
      $data = curl_exec($curl);
      //捕抓异常
      if (curl_errno($curl)) {
          $code = 400; //执行异常
          $data = curl_error($curl);
      }
      curl_close($curl);
      // print_r($data);exit;
      return array('code' => $code, 'data' => json_decode($data, true));
  }
  public  function  sendSms($phone,$code)
  {
    // print_r(urlencode("#code#=1234"));exit;
    $params = [
                  'apikey' => '9616eb7d8bd7ff79ca24af3fad8f192b',
                  'mobile' => $phone,
                  'tpl_id' => '4841936',
                  'tpl_value' =>('#code#').'='.$code
                ];
    $param = '';
    foreach($params as $key => $val){
      $param .= urlencode($key).'='.urlencode($val).'&';
    }
    $param = substr($param,0,strlen($param)-1);

    // print_r($param);//exit;
    $a = $this->send_post("https://sms.yunpian.com/v2/sms/tpl_single_send.json", $param);
    // print_r($a);exit;
  }
  public function getCode($phone,$type)
  {

      $code  = rand(100000,999999);
      $where = [];
      $time  = time()-15*60;

      array_push($where,['code','=',$code]);
      array_push($where,['phone','=',$phone]);
      array_push($where,['type','=',$type]);
      array_push($where,['create_time','>=',$time]);
      $res = Db::name('phone_code')->where($where)->find();
      if ($res) {
        return $this->getCode($phone,$type);
      }
      $addData['code']  = $code;
      $addData['phone'] = $phone;
      $addData['type']  = $type;
      $addData['create_time']  = time();
      Db::name('phone_code')->insert($addData);
      $this->sendSms($phone,$code);
      return ['code'=>200,'status'=>1,'msg'=>'获取成功'];
  }
}
